<template>
  <div>
    <div class="title">
      <el-row :gutter="0">
        <el-col :span="12" :offset="0">
          <span>{{sinfo.titleName}}</span>
        </el-col>
        <el-col :span="12" :offset="0">
          <div class="right">
            <template v-if="this.switch">
              <!-- 因为会有软硬件和工程量清单，不是一个接口不能批量删除 -->
              <!-- <el-button type='danger' size='medium' @click='onDels'>删除选中行</el-button> -->
              <el-button type='green' size='medium' @click='onAdds'>添加IT资产及服务</el-button>
              <el-button type='primary' size='medium' @click='addWord'
                v-if="word_check.indexOf(this.user.company_code)!=-1">添加工程量清单</el-button>
            </template>
            <el-button type='warning' size='medium' @click='editTargetVar'>招标技术参数</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-form :model='searchList' ref='searchList' size='normal'>
      <el-row :gutter='20'>
        <el-col :span='5' :offset='0'>
          <el-input v-model="searchList.name" placeholder="单位名称" size="normal" clearable></el-input>
        </el-col>
        <el-col :span='5' :offset='0'>
          <el-input v-model="searchList.project_name" placeholder="项目名称" size="normal" clearable></el-input>
        </el-col>
        <el-col :span='5' :offset='0'>
          <el-input v-model="searchList.contract_name" placeholder="合同名称" size="normal" clearable></el-input>
        </el-col>
        <el-col :span='5' :offset='0'>
          <el-cascader :options="d_serveAll" v-model="searchList.ITType" clearable class="wid">
          </el-cascader>
        </el-col>
        <el-col :span='4' :offset='0'>
          <div class='right'>
            <el-button type='primary' size='medium' @click='search'>查询</el-button>
            <el-button type='primary' size='medium' @click='reBack'>返回</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <br>
    <el-table :data='tableData.results' border stripe :height='offsetHeight' v-loading='loading' ref='tableData'>
      <el-table-column type='selection' width='50' align='center'>
      </el-table-column>
      <el-table-column type='index' label='序号' width='50' align='center'>
      </el-table-column>
      <el-table-column prop="name" label="单位名称" align='center'></el-table-column>
      <el-table-column prop="project_name" label="项目名称" align='center'></el-table-column>
      <el-table-column prop="contract_name" label="合同名称" align='center'></el-table-column>
      <el-table-column label="名称" align='center' width="120" show-overflow-tooltip>
        <template slot-scope="{row}">
          {{row.word_name ? row.word_name:row.device_name}}
        </template>
      </el-table-column>
      <el-table-column label="类型" width="120" align='center'>
        <template slot-scope="{row}">
          {{row.type | f_ITtypeRename}}
        </template>
      </el-table-column>
      <el-table-column prop="service_pre_amount" label="申请金额" width="80" align='center'></el-table-column>
      <el-table-column prop="third_check_amount" label="审核金额" width="80" align='center'></el-table-column>
      <el-table-column label='操作' width='180' align='center'>
        <template slot-scope='{row}'>
          <el-button-group>
            <!-- 工程量清单、软硬件、购买服务 -->
            <el-button type="primary" size="mini" @click="wordInfo(row)" v-if="row.word_list">查看信息</el-button>
            <el-button type="primary" size="mini" @click="deviceInfo(row)" v-else-if="row.device_name">查看信息
            </el-button>
            <el-button type="primary" size="mini" @click="serveInfo(row)" v-else>查看信息
            </el-button>
            <template v-if="tableSwitch">
              <el-button type="danger" size="mini" @click="removeList(row.id)" v-if="row.word_list">删除
              </el-button>
              <el-button type="danger" size="mini" @click="remove(row.id)" v-else>删除
              </el-button>
            </template>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination @size-change="pageSizeChange" @current-change="pageNoChange" :page-sizes="[10, 50, 100, 200]"
        :page-size="10" background layout="total, sizes, prev, pager, next" :total="tableData.totalRecord">
      </el-pagination>
    </div>
    <!-- 招标技术参数信息 -->
    <el-drawer title="招标技术参数" :visible.sync="addItShow" :before-close="cancel1" size="80%" :destroy-on-close="true"
      :wrapperClosable="false">
      <add-it-item ref="addit" @cancel="cancel" @init="init" :switch="this.switch">
      </add-it-item>
    </el-drawer>
    <!-- 招标技术参数信息 -->
    <el-drawer title="招标技术参数" :visible.sync="targetVarShow" size="70%" :destroy-on-close="true"
      :wrapperClosable="false">
      <target-var-item ref="targetVar" @cancel="cancel" @init="init" :targetVarParams="targetVarParams"
        :switch="this.switch">
      </target-var-item>
    </el-drawer>
    <!-- 工程量清单 -->
    <el-drawer title="工程量清单" :visible.sync="wordShow" size="70%" :destroy-on-close="true" :wrapperClosable="false">
      <word-list-item ref="wordList" @cancel="cancel" @init="init" :form-list="wordParams" :switch="this.switch">
      </word-list-item>
    </el-drawer>
    <!-- 软硬件信息 -->
    <el-drawer title="软、硬件信息" :visible.sync="deviceShow" size="70%" center :destroy-on-close="true"
      :wrapperClosable="false">
      <device-item :device-params="deviceParams" :switch="this.switch" @cancel="cancel" @init="init"></device-item>
    </el-drawer>
    <!-- 购买服务信息 -->
    <el-drawer title="购买服务信息" :visible.sync="serveShow" size="70%" center :destroy-on-close="true"
      :wrapperClosable="false">
      <serve-item :serve-params="serveParams" :switch="this.switch" @cancel="cancel" @init="init"></serve-item>
    </el-drawer>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { d_serveAll } from '@/utils/dicts'
import { f_ITtypeRename } from '@/utils/filters'
import WordListItem from '@/components/Bid/WordList.vue'
import DeviceItem from '@/components/ItUsual/Device'
import ServeItem from '@/components/ItUsual/Serve'
import TargetVarItem from '@/components/Bid/TargetVar.vue'
import AddItItem from '@/components/Bid/AddIt.vue'
export default {
  name: 'EditIt',
  components: {
    WordListItem, DeviceItem, ServeItem, TargetVarItem, AddItItem
  },
  props: {
    switch: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    root: {
      type: String,
      default: () => {
        return 'noFinish'
      }
    },
  },
  data () {
    return {
      offsetHeight: document.documentElement.clientHeight - 335,
      sinfo: {
        titleName: ''
      },
      loading: false,
      searchList: {},
      tableData: [],
      pageNo: '1',
      pageSize: '10',
      d_serveAll: d_serveAll,
      word_check: ['LA00532', 'LA00535', 'LA00588', 'L0002'],
      wordShow: false,
      wordParams: {},
      deviceShow: false,
      deviceParams: {},
      serveShow: false,
      serveParams: {},
      targetVarShow: false,
      targetVarParams: {},
      addItShow: false,
    }
  },
  created () {

  },
  mounted () {

  },
  methods: {
    async init () {
      this.sinfo = JSON.parse(sessionStorage.getItem("bidInfo"));
      this.loading = true
      let type = ''
      if (typeof this.searchList.ITType === 'object') {
        if (this.searchList.ITType.length == 0) {
          type = ''
        } else {
          type = JSON.stringify(this.searchList.ITType)
        }
      }
      const res = await this.$http('blockItPage', {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        subject_id: this.sinfo.id,
        source: this.sinfo.source,
        isSubject: true,
        del_flag: '0',
        ...this.searchList,
        type: type
      })
      this.tableData = res
      this.loading = false
    },
    search () {
      this.pageNo = '1'
      this.pageSize = '10'
      this.init()
    },
    cancel () {
      this.wordParams = {}
      this.wordShow = false
      this.deviceShow = false
      this.deviceParams = {}
      this.serveShow = false
      this.serveParams = {}
      this.targetVarShow = false
      this.targetVarParams = {}
    },
    cancel1 () {
      this.addItShow = false
      this.init()
    },
    addWord () {
      this.wordParams = {
        subject_id: this.sinfo.id,
        source: this.source
      }
      this.wordShow = true
    },
    onAdds () {
      this.addItShow = true
    },
    editTargetVar () {
      this.targetVarParams = {
        id: this.sinfo.id,
      }
      this.targetVarShow = true
    },
    // 有old_id 使用old_id，sorce =2 的都使用old_id查询
    deviceInfo (item) {
      let id = ''
      if (item.old_id) {
        id = item.old_id
      } else {
        id = item.id
      }
      this.deviceShow = true
      this.deviceParams = {
        deviceId: id,
      }
    },
    serveInfo (item) {
      let id = ''
      if (item.old_id) {
        id = item.old_id
      } else {
        id = item.id
      }
      this.serveShow = true
      this.serveParams = {
        serveId: id,
      }
    },
    wordInfo (item) {
      this.wordParams = {
        id: item.id,
        word_name: item.word_name,
        word_list: item.word_list,
        type: JSON.parse(item.type),
        word_type_other: item.word_type_other,
        service_pre_amount: item.service_pre_amount,
        third_check_amount: item.third_check_amount,
      }
      this.wordShow = true
    },
    onDels () {
      if (this.$refs.tableData.selection.length > 0) {
        let ids = []
        this.$refs.tableData.selection.forEach(item => {
          ids.push(item.id)
        })
        this.$confirm('是否确认删除', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          this.onDels1(ids)
        }).catch(() => {

        });
      } else {
        this.$tips.error('请选择至少一条')
      }
    },
    async onDels1 (ids) {
      this.loading = true
      const res = await this.$http('blockItUp', {
        ids: ids,
        del_flag: '1'
      })
      this.$tips.success(res.errmsg)
      this.init()
    },
    remove (id) {
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.remove1(id)
      }).catch(() => {

      });

    },
    async remove1 (id) {
      let ids = []
      ids.push(id)
      this.loading = true
      const res = await this.$http('blockItUp', {
        ids: ids,
      })
      this.$tips.success(res.errmsg)
      this.init()
    },
    removeList (id) {
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.remove1(id)
      }).catch(() => {

      });

    },
    async removeList1 (id) {
      this.loading = true
      const res = await this.$http('blockWordUp', {
        id: id,
        del_flag: '1'
      })
      this.$tips.success(res.errmsg)
      this.init()
    },
    pageNoChange (pageNo) {
      this.pageNo = pageNo
      this.init()
    },
    pageSizeChange (pageSize) {
      this.pageSize = pageSize
      this.init()
    },
    reBack () {
      this.$emit('toIndex', this.switch, this.root);
    },
  },
  computed: {
    ...mapGetters([
      'user'
    ]),
    // unitName () {
    //   if (this.searchList.Name && this.searchList.Name.length > 0) {
    //     return this.searchList.Name[0]
    //   }
    // },
    // projectName () {
    //   if (this.searchList.Name && this.searchList.Name.length > 1) {
    //     return this.searchList.Name[1]
    //   }
    // },
    // contractName () {
    //   if (this.searchList.Name && this.searchList.Name.length > 2) {
    //     return this.searchList.Name[2]
    //   }
    // },
    tableSwitch () {
      return this.switch
    }
  },
  filters: {
    f_ITtypeRename: f_ITtypeRename
  }
}
</script>
<style lang='scss' scoped>
</style>